package com.example.service.impl;

import com.example.entity.Page.MyPageHelper;
import com.example.entity.Page.PageRequest;
import com.example.entity.Page.PageResult;
import com.example.entity.Unit;
import com.example.entity.UnitBuilding;
import com.example.mapper.UnitMapper;
import com.example.result.CommonResult;
import com.example.service.UnitService;
import com.example.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @project: smart-property
 * @description: 单元管理 service 实现层
 * @author: cjq
 * @create: 2022-11-18 10:53
 **/
@Service
public class UnitServiceImpl implements UnitService {
    @Autowired
    UnitMapper unitMapper;

    @Autowired
    RedisUtil redisUtil;

    Integer cid;

    /**
     * 单元名称查重
     * @param unit
     * @param bid
     * @return
     */
    @Override
    public CommonResult listUnit(String unit, Integer bid) {
        Unit unit1 = unitMapper.listUnit(unit, bid);
        if (!StringUtils.isEmpty(unit1)){
            return CommonResult.ok(unit1);
        }
        return CommonResult.fail();
    }

    /**
     * 添加/修改单元信息
     * @param unit
     * @return
     */
    @Override
    public int saveUnitInfo(Unit unit) {
        int i = 0;
        String unitInfo = "UnitInfo"+"_"+this.cid;
        if (unit.getId() == null){
            unit.setUnitNumber(unit.getUnitNumber()+"_"+unit.getUnit());
            unit.setDelFlag(0);
            i = unitMapper.addUnitInfo(unit);
            redisUtil.del(unitInfo);
        }else {
            unit.setUnitNumber(unit.getUnitNumber()+"_"+unit.getUnit());
            unit.setDelFlag(0);
            i = unitMapper.updateUnitInfo(unit);
            redisUtil.del(unitInfo);
        }
        return i;
    }

    /**
     * 单元列表展示
     * @param bid
     * @param unit
     * @param pageRequest
     * @return
     */
    @Override
    public PageResult queyUnitInfo(Integer bid, Integer cid,String unit, PageRequest pageRequest) {
        this.cid = cid;
        String unitInfo = "UnitInfo"+"_"+this.cid;
        List<UnitBuilding> list = (List<UnitBuilding>) redisUtil.get(unitInfo);
        if (bid == null && unit == ""){
            if (list == null){
                list = unitMapper.queyUnitInfo(bid,unit);
                redisUtil.set(unitInfo,list);
            }
        }else {
            return MyPageHelper.getPageResult(unitMapper.queyUnitInfo(bid,unit));
        }
        return MyPageHelper.getPageResult(list);
    }

    /**
     * 删除单元信息
     * @param id
     * @return
     */
    @Override
    public int delUnitInfo(Integer id) {
        String unitInfo = "UnitInfo"+"_"+this.cid;
        redisUtil.del(unitInfo);
        return unitMapper.delUnitInfo(id);
    }

    /**
     * 批量删除单元信息
     * @param ids
     * @return
     */
    @Override
    public int batchDel(Integer[] ids) {
        String unitInfo = "UnitInfo"+"_"+this.cid;
        redisUtil.del(unitInfo);
        return unitMapper.batchDel(ids);
    }
}
